package com.louzhu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.louzhu.common.exception.BizCodeEnum;
import com.louzhu.gulimall.member.exception.PhoneExistException;
import com.louzhu.gulimall.member.exception.UserNameExistException;
import com.louzhu.gulimall.member.vo.MemberLoginVo;
import com.louzhu.gulimall.member.vo.MemberRegisterVo;
import com.louzhu.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.louzhu.gulimall.member.entity.MemberEntity;
import com.louzhu.gulimall.member.service.MemberService;
import com.louzhu.common.utils.PageUtils;
import com.louzhu.common.utils.R;



/**
 * 会员
 *
 * @author jianfneg
 * @email 501039356@qq.com
 * @date 2021-07-15 19:32:25
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 会员注册
     */
    @RequestMapping("/register") // member
    public R register(@RequestBody MemberRegisterVo registerVo) {
        try {
            memberService.register(registerVo);
            //异常机制：通过捕获对应的自定义异常判断出现何种错误并封装错误信息
        } catch (UserNameExistException userExistException) {//用户已存在
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
        } catch (PhoneExistException phoneException) {// 手机已经注册
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
        }
        return R.ok();
    }

    /**
     * 会员登录
     */
    @RequestMapping("/login") // member
    public R login(@RequestBody MemberLoginVo loginVo) {
        MemberEntity entity=memberService.login(loginVo);
        if (entity!=null){
            return R.ok().put("data",entity);
        }else {
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
        }
    }

    /**
     * 社交登录
     */
    @PostMapping("/oauth2/login")
    public R oauth2Login(@RequestBody SocialUser socialUser){
        MemberEntity entity=memberService.login(socialUser);
        if (entity!=null){
            return R.ok().put("data",entity);
        }else {
            return R.error();
        }
    }
}
